package com.jk.lesson3;

import com.jk.lesson2.Apple;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.DoubleSupplier;
import java.util.function.DoubleToIntFunction;

/**
 * Created by Auser on 2016/11/9.
 */
public class ComparatorApple {
    public static void main(String...args){
        List<Apple> apples = Arrays.asList(new Apple("red", 150)
                , new Apple("green", 100)
                , new Apple("red", 130)
                , new Apple("green", 50)
                , new Apple("green", 300)
                , new Apple("green", 270)
                , new Apple("green", 130));
        // way1
        apples.sort(new AppleComparator());

        // way2
        apples.sort(new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getWeight().compareTo(o2.getWeight());
            }
        });

        // way3 Lamdbda
        apples.sort((o1, o2) -> o1.getWeight().compareTo(o2.getWeight()));

        // way4 方法引用
        apples.sort(Comparator.comparing(Apple::getWeight).thenComparing(Apple::getColor));
        System.out.println(apples.toString());

        apples.sort(Comparator.comparing(Apple::getWeight).reversed().thenComparing(Apple::getColor));
        System.out.println(apples.toString());
    }

    static class AppleComparator implements Comparator<Apple> {

        @Override
        public int compare(Apple o1, Apple o2) {
            return o1.getWeight().compareTo(o2.getWeight());
        }
    }

}
